package com.zjp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.zjp.pojo.Reservation;
import com.zjp.pojo.User;

import com.zjp.service.ISiteAndVaccService;
import com.zjp.service.InoculationHistoryService;
import com.zjp.service.ReservationService;
import com.zjp.vo.ISiteAndVaccVo;
import com.zjp.vo.InoculationHistory2;
import com.zjp.vo.NavbarVo;
import com.zjp.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;


import java.util.Date;
import java.util.List;

/**
 * 接种管理
 */
@RestController
@Slf4j
public class InoculationHistoryController {
    @Autowired
    private InoculationHistoryService historyService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ISiteAndVaccService iSiteAndVaccService;

    /**
     * 预约接种
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllISiteAndVacc")
    public ModelAndView findAllISiteAndVacc(HttpSession session, @RequestParam(name = "pageNo", required = false, defaultValue = "1") int pageNo,
                                            @RequestParam(name = "pageSize", required = false, defaultValue = "5") int pageSize,ISiteAndVaccVo iSiteAndVaccVo)  {
        ModelAndView mv=new ModelAndView();
        PageHelper.startPage(pageNo,pageSize);   //开启分页
        List<ISiteAndVaccVo> iSiteAndVaccList=reservationService.findById(iSiteAndVaccVo);
        mv.addObject("iSiteAndVaccList",iSiteAndVaccList);
        PageInfo pageInfo=new PageInfo<>(iSiteAndVaccList);  //存储分页信息
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("iSiteAndVacc");
        return mv;
    }


    /**
     * 确认预约接种
     */
    @RequestMapping("/addReservation")
    public String addReservation(Reservation reservation, Long ivId){
        reservation.setReservationtime(new Date());
        //添加预约信息
        int i=reservationService.insert(reservation);
        log.info("预约接种成功{}",i);
        //判断当疫苗预约成功后，疫苗库存数量减一
        if(i>0){
            iSiteAndVaccService.deleteOne(ivId);
            return "SUCCESS";
        }
        return "FAIL";
    }





    /**
     * 接种记录表
     *
     * @param session
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllInoculationHistory")
    public ModelAndView findAllInoculationHistory(HttpSession session,
                                                  @RequestParam(name = "pageNo", required = false, defaultValue = "1") int pageNo,
                                                  @RequestParam(name = "pageSize", required = false, defaultValue = "5") int pageSize) {
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(pageNo, pageSize);   //开启分页
        //通过用户ID查询当前用到的接种记录
        List<InoculationHistory2> inoculationHistorys =  historyService.findAllInoculationHistory(user.getId());
        log.info("当前用户接种记录为：" + inoculationHistorys);
        mv.addObject("inoculationHistorys", inoculationHistorys);
        PageInfo pageInfo = new PageInfo<>(inoculationHistorys);  //存储分页信息
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("inoculationHistory");
        return mv;
    }

}


